package com.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourism.common.Result;
import com.tourism.entity.ScenicRating;
import com.tourism.mapper.ScenicRatingMapper;
import com.tourism.service.ScenicRatingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class ScenicRatingServiceImpl extends ServiceImpl<ScenicRatingMapper, ScenicRating> implements ScenicRatingService {

    @Override
    public Result getRatingList(Integer page, Integer pageSize, Integer spotId, String userName) {
        try {
            Page<ScenicRating> pageInfo = new Page<>(page, pageSize);

            LambdaQueryWrapper<ScenicRating> queryWrapper = new LambdaQueryWrapper<>();

            // 添加查询条件
            if (spotId != null) {
                queryWrapper.eq(ScenicRating::getSpotId, spotId);
            }
            if (StringUtils.hasText(userName)) {
                queryWrapper.like(ScenicRating::getUserName, userName);
            }

            // 添加排序
            queryWrapper.orderByDesc(ScenicRating::getRatingTime);

            // 执行查询
            Page<ScenicRating> result = this.page(pageInfo, queryWrapper);

            // 打印调试信息
            log.info("查询参数: page={}, pageSize={}, spotId={}, userName={}",
                    page, pageSize, spotId, userName);
            log.info("查询结果: total={}, current={}, size={}, records.size={}",
                    result.getTotal(), result.getCurrent(), result.getSize(),
                    result.getRecords().size());

            return Result.success(result);
        } catch (Exception e) {
            log.error("获取评价列表失败", e);
            return Result.fail("获取评价列表失败: " + e.getMessage());
        }
    }

    @Override
    public Result addRating(ScenicRating scenicRating) {
        try {
            boolean success = this.save(scenicRating);
            return success ? Result.success("") : Result.fail("添加评价失败");
        } catch (Exception e) {
            log.error("添加评价失败", e);
            return Result.fail("添加评价失败: " + e.getMessage());
        }
    }

    @Override
    public Result updateRating(ScenicRating scenicRating) {
        try {
            boolean success = this.updateById(scenicRating);
            return success ? Result.success("") : Result.fail("更新评价失败");
        } catch (Exception e) {
            log.error("更新评价失败", e);
            return Result.fail("更新评价失败: " + e.getMessage());
        }
    }

    @Override
    public Result deleteRating(Integer id) {
        try {
            boolean success = this.removeById(id);
            return success ? Result.success("") : Result.fail("删除评价失败");
        } catch (Exception e) {
            log.error("删除评价失败", e);
            return Result.fail("删除评价失败: " + e.getMessage());
        }
    }

    @Override
    public Result batchDeleteRatings(List<Integer> ids) {
        try {
            boolean success = this.removeByIds(ids);
            return success ? Result.success("") : Result.fail("批量删除评价失败");
        } catch (Exception e) {
            log.error("批量删除评价失败", e);
            return Result.fail("批量删除评价失败: " + e.getMessage());
        }
    }

    @Override
    public Result getAverageRating(Integer spotId) {
        try {
            // 计算平均分
            LambdaQueryWrapper<ScenicRating> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ScenicRating::getSpotId, spotId);

            double avgRating = this.list(queryWrapper)
                    .stream()
                    .mapToInt(ScenicRating::getRating)
                    .average()
                    .orElse(0.0);

            return Result.success(avgRating);
        } catch (Exception e) {
            log.error("获取平均评分失败", e);
            return Result.fail("获取平均评分失败: " + e.getMessage());
        }
    }
}